package edu.tum.cs.stageflow.language.ui.views.schedule

import org.eclipse.jface.viewers.AbstractTableViewer
import org.eclipse.jface.viewers.TableLayout
import org.eclipse.jface.viewers.ViewerRow
import org.eclipse.swt.SWT
import org.eclipse.swt.graphics.Point
import org.eclipse.swt.widgets.Composite
import org.eclipse.swt.widgets.Item
import org.eclipse.swt.widgets.Table
import org.eclipse.swt.widgets.TableColumn
import org.eclipse.swt.widgets.TableItem
import org.eclipse.swt.widgets.Widget

class ScheduleTable extends AbstractTableViewer {

	private Table table

	private ScheduleTableViewerRow cachedRow;

	new(Composite parent) {
		this(parent, SWT.MULTI.bitwiseOr(SWT.H_SCROLL.bitwiseOr(SWT.V_SCROLL.bitwiseOr(SWT.BORDER))))
	}

	new(Composite parent, int style) {
		this(new Table(parent, style))
	}

	new(Table table) {
		this.table = table
		hookControl(table)

		for (i : 0 ..< 5) {
			var TableColumn item = new TableColumn(table, SWT.NULL)
			item.setText("Col " + i)
			item.setWidth(100)
			//item.pack()
		}

		var TableLayout layout = new TableLayout()
		table.setLayout(layout)
		table.setHeaderVisible(true)
		table.setLinesVisible (true)

		for (i : 0 ..< 128) {
			var TableItem  item = new TableItem (table, SWT.NONE)
			item.setText (0, "s");
			item.setText (1, "a");
			item.setText (2, "y");
			item.setText (3, "Whaaaaaaaaaaaaaaaaaaaaaaaaat");
			item.setText (4, "?");
		}
	}

	override protected doClear(int index) {
		table.clear(index)
	}

	override protected doClearAll() {
		table.clearAll
	}

	override protected doDeselectAll() {
		table.selectAll
	}

	override protected doGetColumn(int index) {
		return table.getColumn(index)
	}

	override protected doGetItem(int index) {
		return table.getItem(index)
	}

	override protected doGetItemCount() {
		return table.getItemCount()
	}

	override protected doGetItems() {
		table.items
	}

	override protected doGetSelection() {
		table.selection
	}

	override protected doGetSelectionIndices() {
		table.selectionIndices
	}

	override protected doIndexOf(Item item) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	override protected doRemove(int[] indices) {
		table.remove(indices)
	}

	override protected doRemove(int start, int end) {
		table.remove(start, end)
	}

	override protected doRemoveAll() {
		table.removeAll()
	}

	override protected doResetItem(Item item) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	override protected doSelect(int[] indices) {
		table.select(indices)
	}

	override protected doSetItemCount(int count) {
		table.setItemCount(count)
	}

	override protected doSetSelection(Item[] items) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	override protected doSetSelection(int[] indices) {
		table.setSelection(indices)
	}

	override protected doShowItem(Item item) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	override protected doShowSelection() {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	override protected internalCreateNewRowPart(int style, int rowIndex) {
		var TableItem tItem

		if (rowIndex >= 0) {
			tItem = new TableItem(table, style, rowIndex)
		} else {
			tItem = new TableItem(table, style)
		}
		return new ScheduleTableViewerRow(tItem as TableItem)
	}

	override protected createViewerEditor() {
		return null
	}

	override protected doGetColumnCount() {
		return table.columnCount
	}

	override protected getItemAt(Point point) {
		table.getItem(point)
	}

	override protected getViewerRowFromItem(Widget item) {
		if (cachedRow == null) {
			cachedRow = new ScheduleTableViewerRow(item as TableItem);
		} else {
			cachedRow.setItem(item as TableItem);
		}

		return cachedRow;
	}

	override getControl() {
		return table
	}

}
